<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="目录管理">

    <!--suppress ALL -->
    <form id="searchForm" class="form-panel" action="${basepath}/rest/manage/testprojectcategory/loadData">
        <a href="${basepath}/rest/manage/testproject/toList" class="backPage">返回上一级</a>
        <ul class="panel-content">
            <li>
                <div class="form-actions">
                    <div class="control-group controls span5">
                        <input type="text" id="searchForm" class="input-normal  input-pad" name="name"
                               placeholder="请输入关键字搜索">
                    </div>
                    <button type="submit" class="button  button-primary">
                        查询
                    </button>
                    <a class="button button-primary" style="margin-left: 20px;" onclick="return addFunction()">
                        新增
                    </a>
                    <a class="button button-danger" style="margin-left: 20px;" onclick="return delFunction()">
                        批量删除
                    </a>

                </div>
            </li>
        </ul>
    </form>
<#----------------------------------------------新增表单start--------------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/rest/manage/testprojectcategory/insertJson" method="post">
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">目录名称：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name"
                           data-rules="{required : true}">
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------新增表单 end---------------------------------------------------------->
<#----------------------------------------------编辑表单start--------------------------------------------------------->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal" action="${basepath}/rest/manage/testprojectcategory/updateJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id"/>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">目录名称：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name"
                           data-rules="{required : true}">
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------编辑表单 end---------------------------------------------------------->
<div id="grid"></div>
<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {
                    title: '序号',
                    elCls: 'center',
                    dataIndex: 'id',
                    width: '10%',
                    renderer: function (value, obj, index) {
                        return index + 1;
                    }
                },
                {title: '目录名称', dataIndex: 'name', elCls: 'center', width: '70%'},
                {
                    title: '操作',
                    dataIndex: 'id',
                    elCls: 'center',
                    width: '20%',
                    renderer: function (value, obj, index) {
                        return '<a href="javascript:edit(' + value + ')">编辑</a>&nbsp;&nbsp;<a href="javascript:deleteOne(' + value + ')">删除</a>';
                    }
                }
            ];

    var store = new Store({
                url: '${basepath}/rest/manage/testprojectcategory/loadData',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();


    //删除选中的记录
    function delFunction() {
        var selections = grid.getSelection();
        var ids = new Array();
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        $.ajax({
            type: "POST",
            url: "${basepath}/rest/manage/testprojectcategory/deletesJson",
            dataType: "json",
            data: {
                ids: ids
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }
    /**********************删除单个信息*************************/
    function deleteOne(value) {
        BUI.Message.Confirm('确定要执行删除操作？', function () {
            $.ajax({
                type: 'POST',
                url: '${basepath}/rest/manage/testprojectcategory/deleteJson',
                dataType: 'json',
                data: {
                    id: value
                },
                success: function () {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }
    /*------------------------------------------------新增表单处理start------------------------------------------------*/
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '添加目录',
        width: 400,
        height: 130,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.submit();
            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function addFunction() {
            addForm.clearFields();
            addDialog.show();
    }
    /*------------------------------------------------新增表单处理 end-------------------------------------------------*/
    /*------------------------------------------------编辑表单处理 start-----------------------------------------------*/
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();

    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑目录',
        width: 400,
        height: 130,
        contentId: 'editContent',
        success: function () {
            if (editForm.isValid()) {
                editForm.submit();
            } else {
                editForm.valid();
            }
        }
    });

    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "${basepath}/rest/manage/testprojectcategory/toEditJson",
            data: {id: id},
            success: function (data) {
                var form = $("#editForm")[0];
                BUI.FormHelper.setFields(form, data.data);
                editDialog.show();
            }
        });
    }
    /*------------------------------------------------编辑表单处理 end------------------------------------------------*/
    /**********************模糊搜索*************************/
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
</script>

</@page.pageBase>
